package com.demo.netty.util;

import java.util.Random;
import java.util.concurrent.TimeUnit;
 
class House {
    int saleCount = 0;
    public synchronized void saleHouse() {
        ++saleCount;
    }
}
 
/**
 * 需求1: 5个销售卖房子, 集团高层只关心销售总量的准确计数
 */
public class ThreadLocalDemo {
    public static void main(String[] args) {
        House house = new House();
        for(int i = 1; i<=5; i++) {
            new Thread(()->{
                int size = new Random().nextInt(5) + 1;
                System.out.println(size);
                for(int j = 0; j<size; j++) {
                    house.saleHouse();
                }
            }, String.valueOf(i)).start();
        }
 
        //暂停毫秒
        try {
            TimeUnit.MICROSECONDS.sleep(300);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
 
        System.out.println(Thread.currentThread().getName() + "\t" + "共计卖出 " + house.saleCount + " 套");
    }
}